/*
 * IpHeader.h
 *
 *  Created on: Oct 14, 2008
 *      Author: chaosun
 */
#include "iu_wiretap.h"
using namespace std;
extern int dflag;

#ifndef IPHEADER_H_
#define IPHEADER_H_

#include "../tests/IpHeaderTest.h"

/* IP header */
struct sniff_ip {
  u_int ip_hl:4, /* both fields are 4 bits */
        ip_v:4;
  u_char ip_tos;
  u_short ip_len;
  u_short ip_id;
  u_short ip_off;
#define IP_RF 0x8000
#define IP_DF 0x4000
#define IP_MF 0x2000
#define IP_OFFMASK 0x1fff
  u_char ip_ttl;
  u_char ip_p;
  u_short ip_sum;
  struct in_addr ip_src, ip_dst;
};

//int countBroadcast = 0;

class IpHeader
{
public:
  IpHeader();
  friend class IpHeaderTest;
  static int id;
  static int countBroadcast;
  static string current_source;
  static string current_destination;
  static u_char source_byte[4];
  static u_char dest_byte[4];
  static u_char protocol[2];
  static u_short data_len;
  /**
   * AfindById. find the corresponding IP address string from a given id
   * \param id is an address id which is instinct for a certain IP address
   * \return the corresponding IP address string from the given id
   */
  static string findById(int id);
  /**
   * AlreadyIn. Check if the new IP address has been added to the vector.
   * \param ip_ is a pointer to the IP header struct
   * \param ipType is an integer indicating the type of the address
   * \return the position of the existing IP address, or -1 if it is not in the vector.
   */
  static int alreadyIn(struct sniff_ip *ip_, int ipType);
  /**
   * strCompare. Compare a string and a char array
   * \param str2 is a char array containing a new address for comparison with a given string
   * \return 0 if the two are the same
   */
  int strCompare(char * str2);
  /**
    * Packet handler for IP Header
    * \param packet is a pointer for the whole packet
    * \param index is an index which indicate start of IP header
    * \return next protocol
    */
  static Protocol handlePacket(const u_char * packet, int& index);
  string printIP();
  static string showAll();
  virtual
  ~IpHeader();
private:
  int src_nIP_;
  int dst_nIP_;
  string ipAddr_;
};
static vector <IpHeader> ipVector;
static vector <string> broadcast_srcVector;

#endif /* IPHEADER_H_ */


